/*=========================================================================
 *  Copyright 2009 NCS Pte. Ltd. All Rights Reserved
 *
 *  This software is confidential and proprietary to NCS Pte. Ltd. You shall
 *  use this software only in accordance with the terms of the license
 *  agreement you entered into with NCS.  No aspect or part or all of this
 *  software may be reproduced, modified or disclosed without full and
 *  direct written authorisation from NCS.
 *
 *  NCS SUPPLIES THIS SOFTWARE ON AN AS IS BASIS. NCS MAKES NO
 *  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
 *  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
 *  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 *  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *  Change Revision
 *  -------------------------------------------------------------------------
 *  May 28, 2009  yunfeng   Description
 */
package canglang.monitor.core.base.converter;

import java.util.Date;
import java.util.Map;

import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.properties.AppDefaultProperty;

@SuppressWarnings("unchecked")
public class DateConverter extends ognl.DefaultTypeConverter {

	public Object convertValue(Map context, Object value, Class toType) {
		if (toType == Date.class) {
			if (((String[]) value)[0] == null
					|| ((String[]) value)[0].trim().equalsIgnoreCase(""))
				return null;
            String s=((String[]) value)[0];
			return DateUtil.convertParse(s.substring(8,10)+"/"+s.substring(5,7)+"/"+s.substring(0,4));

		} else if (toType == String.class) {
			if (value == null)
				return "";
            String s=DateUtil.formatDate((Date) value);
			return s.substring(6,10)+ AppDefaultProperty.getProperty("label.year") +s.substring(3,5)+AppDefaultProperty.getProperty("label.month")+s.substring(0,2)+AppDefaultProperty.getProperty("label.day");
		}
		return null;
	}
}
